// 修改密码弹窗
const changePswmodal = `<div class="modal-dialog" role="document">  
<div class="modal-content">  
    <div class="modal-header">  
        <button type="button" class="close" id="closebutton" aria-label="Close">
          <span  aria-hidden="true">×</span></button>  
        <h4 class="modal-title" id="exampleModalLabel">修改密码</h4>  
    </div>  
    <div class="modal-body">  
        <form id="changePswForm">  
            <div class="form-group"> 
              <label for="recipient-name" class="control-label" >旧密码:</label>  
              <input type="text" class="form-control " id="oldPsw" name="oldPsw"> 
            </div>  
            <div class="form-group">  
                <label for="message-text" class="control-label">新密码:</label>  
                <input type="password" class="form-control" id="newPsw" name="newPsw">  
            </div>  
            <div class="form-group">  
              <label for="message-text" class="control-label">再次输入新密码:</label>  
              <input type="password" class="form-control" id="renewPsw" name="renewPsw">   
            </div>  
        </form>  
    </div>  
    <div class="modal-footer">  
        <button type="button" class="btn btn-default" id="disscardChange">放弃修改</button>  
        <button type="button" class="btn btn-primary" id="confirmChange">确定</button>  
    </div>  
</div>`

// 修改密码
function changePsw() {
  $('body').on('click', '#changePsw', function () {
    if ($('#modal').children().length != 0) {
      $('#modal div').remove()
    }
    $('#modal').append(changePswmodal)
    $('#modal').modal('show')

    // x按钮，关闭表单，清空数据
    $('#modal').on('click', '#closebutton', function () {
      $('#modal ').modal('hide')
      $('#modal .div').remove()
    })
    // 取消修改
    $('#modal').on('click', '#disscardChange', function () {
      $('#modal').modal('hide')
      $('#modal .div').remove()
    })
    // 修改密码表单验证
    $('#changePswForm').bootstrapValidator({
      message: 'This value is not valid',
      feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh',
      },
      fields: {
        oldPsw: {
          message: '验证失败',
          validators: {
            notEmpty: {
              message: '密码不能为空',
            },
            stringLength: {
              min: 5,
              max: 18,
              message: '长度必须在5到18位之间',
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_]+$/,
              message: '只能包含大写、小写、数字和下划线',
            },
          },
        },
        newPsw: {
          message: '验证失败',
          validators: {
            notEmpty: {
              message: '密码不能为空',
            },
            stringLength: {
              min: 5,
              max: 18,
              message: '密码长度必须在6到18位之间',
            },
            regexp: {
              regexp: /^[a-zA-Z0-9_]+$/,
              message: '密码只能包含大写、小写、数字和下划线',
            },
            different: {
              field: 'oldPsw',
              message: '不能和旧密码重合',
            },
          },
        },
        renewPsw: {
          validators: {
            notEmpty: {
              message: '请再次输入密码!',
            },
            identical: {
              //用来判断制定的字段和当前字段一致与否
              field: 'newPsw',
              message: '两次输入的密码不一致!',
            },
          },
        },
      },
    })

    // 提交修改
    $('#modal').on('click', '#confirmChange', function () {
      const changePswValidator = $('#changePswForm').data('bootstrapValidator')
      changePswValidator.validate()
      const oldPsw = $('#oldPsw').val()
      const newPsw = $('#newPsw').val()
      const username = $('#user').text()
      const encodeoldPas = md5encode(username + md5encode(oldPsw))
      const encodenewPas = md5encode(username + md5encode(newPsw))
      $.ajax({
        type: 'POST',
        url: 'http://localhost:3000/users/changepsw', // 获取自己系统后台用户信息接口
        data: JSON.stringify({
          oldPsw: encodeoldPas,
          newPas: encodenewPas,
          username: username,
        }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
          if (data) {
            if (data.code == '0') {
              //判断返回值，这里根据的业务内容可做调整
              setTimeout(function () {
                //做延时以便显示登录状态值
                alert('修改成功,去登录!')
                //指向登录的页面地址
                window.location.replace('./login/login.html')
              }, 100)
            } else {
              alert(data.message) //显示登录失败的原因
              return false
            }
          }
        },
        error: function (data) {
          alert(data.message)
        },
      })
      $('#modal').modal('hide')
      $('#modal .div').remove()
    })
  })
}

// md5加密
function md5encode(word) {
  return CryptoJS.MD5(word).toString()
}
